package com.lbh.cloudgallery.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbh.cloudgallery.entity.OrderEntity;
import com.lbh.cloudgallery.entity.OrderItemEntity;
import com.lbh.cloudgallery.entity.vo.OrderResult;
import com.lbh.cloudgallery.entity.vo.OrderStatusVo;
import com.lbh.cloudgallery.service.OrderItemService;
import com.lbh.cloudgallery.service.OrderService;
import com.lbh.cloudgallery.entity.vo.OrderItemVos;
import com.xxx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 
 *
 * @author 廖碧涵
 * @email lbh@qq.com
 * @date 2021-02-25 22:47:51
 */
@Api(tags = "订单")
@RestController
@RequestMapping("front/order")
public class FrontOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;


    @ApiOperation(value = "提交订单")
    @PostMapping("/createOrder")
    public R createOrder(@RequestBody OrderItemVos vo){

        Long orderId = orderService.createOrder(vo);

        if(orderId!=null){

            return R.ok().put("orderId",orderId);
        }
        return R.error().put("msg","创建订单失败");
    }


    @ApiOperation(value = "查询订单详情")
    @GetMapping("/orderList/{orderId}")
    public R orderList(@PathVariable("orderId") Long orderId){

        OrderEntity orderEntity = orderService.getById(orderId);

        List<OrderItemEntity> orderItemEntities = orderItemService.list(new QueryWrapper<OrderItemEntity>().eq("order_id", orderId));

        return R.ok().put("order",orderEntity).put("items",orderItemEntities);

    }



    @ApiOperation(value = "根据状态查询订单")
    @PostMapping("/allOrderList")
    public R allOrderList(@RequestBody OrderStatusVo vo){
        List<OrderEntity> memberOrder =new ArrayList<>();
        if (vo.getStatus()==null){
            //查出该用户所拥有的订单
            memberOrder = orderService.list(new QueryWrapper<OrderEntity>().eq("member_id", vo.getMemberId()));

        }else {
            memberOrder = orderService.list(new QueryWrapper<OrderEntity>().eq("member_id", vo.getMemberId()).eq("status",vo.getStatus()));
        }
        //流式编程一个购物单与很多购物项绑定
        Map<Long, OrderResult> orders = memberOrder.stream().collect(Collectors.toMap(k->k.getId(), v -> {
            OrderResult orderResult = new OrderResult();
            List<OrderItemEntity> orderItem = orderItemService.list(new QueryWrapper<OrderItemEntity>().eq("order_id", v.getId()));
            orderResult.setStatus(v.getStatus());
            orderResult.setOrderItem(orderItem);
            return orderResult;
        }));
        return R.ok().put("data",orders);

    }



    @ApiOperation(value = "修改订单状态")
    @GetMapping("/orderStatus/{orderId}/{status}")
    public R orderStatus(@PathVariable("orderId") Long orderId,@PathVariable("status") Integer status){
        boolean b = orderService.changeStatus(orderId,status);
        if (b){
            return R.ok().put("status",status);
        }else return R.error();
    }
}
